﻿
// MFCApplication1View.cpp: CMFCApplication1View 类的实现
//

#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "MFCApplication1.h"
#endif

#include "MFCApplication1Doc.h"
#include "MFCApplication1View.h"
#include <cmath>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMFCApplication1View

IMPLEMENT_DYNCREATE(CMFCApplication1View, CView)

BEGIN_MESSAGE_MAP(CMFCApplication1View, CView)
	// 标准打印命令
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(ID_POPUP_POLYGON, &CMFCApplication1View::OnPopupPolygon)
	ON_COMMAND(ID_POPUP_RECTANGLE, &CMFCApplication1View::OnPopupRectangle)
	ON_WM_KEYDOWN()
	ON_WM_TIMER()
	ON_WM_MOUSEMOVE()
	ON_COMMAND(ID_SPEEDUP, &CMFCApplication1View::OnSpeedup)
	ON_COMMAND(ID_SLOWDOWN, &CMFCApplication1View::OnSlowdown)
END_MESSAGE_MAP()

// CMFCApplication1View 构造/析构

CMFCApplication1View::CMFCApplication1View() noexcept
{
	// TODO: 在此处添加构造代码
	acting = false;
	//Timer默认时间间隔
	timeC = 500;
	movx = movy = 8;
	rectxy = new int[4];
	//矩形坐标250,50 550,350
	rectxy[0] = 250;
	rectxy[1] = 50;
	rectxy[2] = 554;
	rectxy[3] = 354;

	//六边形坐标250,50 490,50 650,210 490,370 250,370 90,210
	polyxy = new CPoint[6];
	polyxy[0].x = 250; polyxy[0].y = 50;
	polyxy[1].x = 490; polyxy[1].y = 50;
	polyxy[2].x = 650; polyxy[2].y = 210;
	polyxy[3].x = 490; polyxy[3].y = 370;
	polyxy[4].x = 250; polyxy[4].y = 370;
	polyxy[5].x = 90; polyxy[5].y = 210;
}

CMFCApplication1View::~CMFCApplication1View()
{
	delete[] rectxy;
}

BOOL CMFCApplication1View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CView::PreCreateWindow(cs);
}

// CMFCApplication1View 绘图

void CMFCApplication1View::OnDraw(CDC* /*pDC*/)
{
	CMFCApplication1Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	CButton btn;
	
}


// CMFCApplication1View 打印

BOOL CMFCApplication1View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// 默认准备
	return DoPreparePrinting(pInfo);
}

void CMFCApplication1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加额外的打印前进行的初始化过程
}

void CMFCApplication1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加打印后进行的清理过程
}


// CMFCApplication1View 诊断

#ifdef _DEBUG
void CMFCApplication1View::AssertValid() const
{
	CView::AssertValid();
}

void CMFCApplication1View::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMFCApplication1Doc* CMFCApplication1View::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMFCApplication1Doc)));
	return (CMFCApplication1Doc*)m_pDocument;
}
#endif //_DEBUG


// CMFCApplication1View 消息处理程序


void CMFCApplication1View::OnRButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	CMenu m;
	m.CreatePopupMenu();
	//添加浮动菜单的菜单项
	m.AppendMenuW(MF_ENABLED, ID_POPUP_RECTANGLE, L"Rectangle");
	m.AppendMenuW(MF_ENABLED, ID_POPUP_POLYGON, L"Polygon");
	CPoint pt;
	GetCursorPos(&pt);
	m.TrackPopupMenu(TPM_LEFTALIGN,pt.x,pt.y,this);
	CView::OnRButtonDown(nFlags, point);
}


void CMFCApplication1View::OnPopupPolygon()
{
	if (acting) {
		KillTimer(1);
		acting = false;
	}	
	//true表示选择了六边形
	select = true;
	//客户区刷新
	CRect rect;
	GetClientRect(&rect);
	InvalidateRect(&rect);
	UpdateWindow();
	//先设置位图的初始位置
	CDC* pDC = GetDC();
	pDC->Polygon(polyxy, 6);
	x = polyxy[0].x;
	y = polyxy[0].y;
	//异或画刷画初始位图
	CBitmap bm;
	bm.LoadBitmap(IDB_BITMAP1);
	CBrush newBrush;
	newBrush.CreatePatternBrush(&bm);
	pDC->SelectObject(newBrush);
	pDC->PatBlt(x - 4, y - 4, 8, 8, PATINVERT);
	CString str;
	str.Format(L"Poly");
	//获得状态栏的指针
	CStatusBar* pStatusBar = (CStatusBar*)GetParentFrame()->GetDescendantWindow(ID_VIEW_STATUS_BAR);
	//在状态栏的第二个窗格中输出当前状态
	pStatusBar->SetPaneText(1, str);
}


void CMFCApplication1View::OnPopupRectangle()
{
	if (acting) {
		KillTimer(1);
		acting = false;
	}
	//false表示选择了矩形
	select = false;
	//刷新客户区
	CRect rect;
	GetClientRect(&rect);
	InvalidateRect(&rect);
	UpdateWindow();
	//设置位图初始位置
	CDC* pDC = GetDC();
	pDC->Rectangle(rectxy[0], rectxy[1], rectxy[2], rectxy[3]);
	x = rectxy[0];
	y = rectxy[1];
	//异或画刷画初始位图
	CBitmap bm;
	bm.LoadBitmap(IDB_BITMAP1);
	CBrush newBrush;
	newBrush.CreatePatternBrush(&bm);
	pDC->SelectObject(newBrush);
	pDC->PatBlt(x - 4, y - 4, 8, 8, PATINVERT);
	CString str;
	str.Format(L"Rec");
	//获得状态栏的指针
	CStatusBar* pStatusBar = (CStatusBar*)GetParentFrame()->GetDescendantWindow(ID_VIEW_STATUS_BAR);
	//在状态栏的第二个窗格中输出当前状态
	pStatusBar->SetPaneText(1, str);
}


void CMFCApplication1View::clear()
{
	//获取设备环境
	CDC* pDC = GetDC();
	CRect rect;
	//获取客户区
	GetClientRect(&rect);
	CBrush brush(RGB(255, 255, 255));
	//笔刷填充白色
	pDC->FillRect(&rect, &brush);
}


void CMFCApplication1View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	//热键消息槽
	if (nChar == 'V') {
		if (acting) {
			//如果在运行，按v后停止计时，状态acting设置为false
			KillTimer(1);
			acting = false;
		}
		else {
			acting = true;
			SetTimer(1, timeC, NULL);
		}
	}

	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}


void CMFCApplication1View::OnTimer(UINT_PTR nIDEvent)
{
	//计时器1响应
	if (nIDEvent == 1) {
		CBitmap bm; 
		bm.LoadBitmap(IDB_BITMAP1);
		CBrush newBrush;
		newBrush.CreatePatternBrush(&bm);
		CDC* pDC = GetDC();
		pDC->SelectObject(newBrush);
		//异或画刷，消除上一个位置的图形
		pDC->PatBlt(x-4, y-4,  8,  8, PATINVERT);


		//更改移动方向
		if (select) {
			/*float stepx = (polyxy[state % 6].x - polyxy[(state - 1) % 6].x) / 20;
			float stepy = (polyxy[state % 6].y - polyxy[(state - 1) % 6].y) / 20;
			x += stepx;
			y += stepy;

			if (abs(polyxy[state % 6].x - x)< stepx&& abs(polyxy[state % 6].y - y) < stepy)
				state = (state + 1) % 7;*/
			//六边形250,50 490,50 650,210 490,370 250,370 90,210
			//边界条件判断，改变移动方向
			if (x > 242 && x <= 482 && y < 58) {
				movx = 8;
				movy = 0;
			}
			else if (x > 482 && y < 58) {
				movx = 8;
				movy = 8;
			}
			else if (x > 642 && y > 202) {
				movx = -8;
				movy = 8;
			}
			else if (x < 498 && x >= 258 && y > 362) {
				movx = -8;
				movy = 0;
			}
			else if (x < 258 && y > 362) {
				movx = -8;
				movy = -8;
			}
			else if (x < 98 && y < 218){
				movx = 8;
				movy = -8;
			}
		}
		else {
			//矩形250,50 554,354
			if (x < 258 && y < 58) {
				movx = 8;
				movy = 0;
			}
			else if (x > 546 && y < 58) {
				movx = 0;
				movy = 8;
			}
			else if (x > 546 && y > 346) {
				movx = -8;
				movy = 0;
			}
			else if (x < 258 && y >346) {
				movx = 0;
				movy = -8;
			}
		}
		x += movx;
		y += movy;
		pDC->PatBlt(x - 4, y - 4,  8,  8, PATINVERT);
		bm.DeleteObject();
		//状态栏更新位图坐标
		CString str;
		str.Format(L"x = %d,y = %d  ", x, y);
		GetParent()->GetDescendantWindow(AFX_IDW_STATUS_BAR)->SetWindowText(str);
	}
	CView::OnTimer(nIDEvent);
}


/*void CMFCApplication1View::OnMouseMove(UINT nFlags, CPoint point)
{


	CView::OnMouseMove(nFlags, point);
}*/


void CMFCApplication1View::OnSpeedup()
{
	//增加速度，判断是否超限，调整Timer间隔
	if (timeC > 70) {
		timeC -= 70;
		KillTimer(1);
		SetTimer(1, timeC, NULL);
	}
	else
		MessageBox(L"Max Speed!");
}


void CMFCApplication1View::OnSlowdown()
{
	//降低速度，判断是否超限，调整Timer间隔
	if (timeC < 710) {
		timeC += 70;
		KillTimer(1);
		SetTimer(1, timeC, NULL);
	}
	else
		MessageBox(L"Min Speed!");
}
